package guiscreens;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import dao.ExecuteQuery;

import javax.swing.JTextField;
import javax.swing.BoxLayout;


public class login extends JFrame {

	private JPanel contentPane;
	public static final int XCENTER = Toolkit.getDefaultToolkit().getScreenSize().width/2;
	public static final int YCENTER = Toolkit.getDefaultToolkit().getScreenSize().height/2;
	private CardLayout c1;
	private JPanel basePanel;
	private JTextField textField;
	private JPasswordField textField_1;
	private JFrame thisFrame;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					login frame = new login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public login() {
		thisFrame = this;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("GTBay Login");
		setBounds(XCENTER - 250, YCENTER - 190, 500, 380);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		JPanel panel = new JPanel();
		
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		panel.setBackground(Color.WHITE);
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setBackground(Color.WHITE);
		
		JLabel lblItsNotJunk = new JLabel("It's not junk if someone will pay for it");
		ImageIcon icon = new ImageIcon("jacket.gif");
		JLabel jacket = new JLabel(icon);
		jacket.setPreferredSize(new Dimension(100, 100));
		panel_1.add(jacket);
		panel_1.add(lblItsNotJunk);
		
		basePanel = new JPanel();
		c1 = new CardLayout();
		basePanel.setLayout(c1);
		panel.add(basePanel, BorderLayout.CENTER);
		panel.setName("BASECARD");
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		basePanel.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(35dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(89dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(19dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(42dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblUsername = new JLabel("Username  ");
		panel_2.add(lblUsername, "8, 4, right, default");
		
		textField = new JTextField();
		panel_2.add(textField, "10, 4, 9, 1, fill, default");
		textField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password  ");
		panel_2.add(lblPassword, "8, 8, right, default");
		
		textField_1 = new JPasswordField();
		panel_2.add(textField_1, "10, 8, 9, 1, fill, default");
		textField_1.setColumns(10);
		
		JButton btnRegister = new JButton("Register");
		panel_2.add(btnRegister, "14, 12");
		btnRegister.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				Register register = new Register(c1, basePanel);
				basePanel.add(register, "REGISTERCARD");
				c1.show(basePanel, "REGISTERCARD");
			}
		});
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				if (textField.getText().equals("") || textField_1.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null, "Please enter your username"
							+ " and password");
				}
				else {
					if (ExecuteQuery.login(textField.getText(), 
							textField_1.getText()) == true)
					{
						MainWindow window = new MainWindow(textField.getText());
						thisFrame.dispose();
					}
					
					else 
					{
						JOptionPane.showMessageDialog(null, "Login Failed");
					}
				}
			}
		});
		panel_2.add(btnLogin, "18, 12");
	}
}
